package com.yj.suncity.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.Utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class UpdatePwdActivity2 extends Activity {

	private Context context;
	private String phone;
	private EditText mEdt_pwd1, mEdt_pwd2;
	private List<NameValuePair> pairs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_pwd2);
		context = this;
		phone = getIntent().getStringExtra("phone");
		pairs = new ArrayList<NameValuePair>();
		findView();
	}

	private void findView() {
		mEdt_pwd1 = (EditText) findViewById(R.id.edt_pwd1);
		mEdt_pwd2 = (EditText) findViewById(R.id.edt_pwd2);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.btn_ok:
			String pwd1 = mEdt_pwd1.getText().toString().trim();
			String pwd2 = mEdt_pwd2.getText().toString().trim();
			if (pwd1.equals("")) {
				Utils.showToast(context, "请输入新密码！");
				return;
			}
			if (!pwd2.equals(pwd1)) {
				Utils.showToast(context, "两次输入密码不一样！");
				return;
			}
			UpdatePwd(pwd1);
			break;
		default:
			break;
		}

	}

	private void UpdatePwd(String pwd) {
		pairs.clear();
		String path = URLCon.用户;
		pairs.add(new BasicNameValuePair("action", "modifypassword"));
		pairs.add(new BasicNameValuePair("mobile", phone));
		pairs.add(new BasicNameValuePair("password", pwd));

		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, pairs, null,
				false, null, false, "", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							Utils.showToast(context,
									"" + jsonObject.optString("msg"));
							setResult(1);
							UpdatePwdActivity2.this.finish();
						} else {
							Utils.showToast(context,
									"" + jsonObject.optString("msg"));
						}

					}
				});
		asyncTaskUtil.execute(path);
	}

}
